import CommonUtils from "./common-utils.js";
import http from "../http/http.js";
import api from "../http/api.js";
import CRC from './crc.js'
export default {
    //修改IOT测点指令
    format11Cmd(deviceId, index, value) {
        var length = 6;
        var valueHex = CommonUtils.str_pad("00", value.toString(16));
        if (this.index == 6 || this.index == 7) {
            length = 7;
            valueHex = CommonUtils.str_pad("0000", (value * 10).toString(16));
        }
        var cmd = '5A';
        var verCode = '4D5311' + CommonUtils.str_pad("0000", length.toString(16));
        verCode += CommonUtils.str_pad("00000000", deviceId.toString(16));
        verCode += CommonUtils.str_pad("00", index.toString(16));
        verCode += valueHex;
        cmd += verCode;
        cmd += CRC.ToCRC16(CommonUtils.str2Bytes(verCode));
        cmd += '4D';
        return cmd;
    },

    //下发事件（不带数据）
    format14Cmd(deviceId, index) {
        var cmd = '5A';
        var verCode = '4D53140005'
        verCode += '00000000'; //设备id
        verCode += CommonUtils.str_pad("00", index.toString(16));
        cmd += verCode;
        cmd += CRC.ToCRC16(CommonUtils.str2Bytes(verCode));
        cmd += '4D';
        return cmd;
    },

    sendCmdDevice(deviceId, cmd, complete) {
        http.post(api.SEND_CMD_DEVICE, {
            deviceId: deviceId,
            cmd: cmd
        }, data => {
            if (complete) complete();
        })
    },
    sendBatchCmd(adminId, cmd, complete) {
        http.post(api.SEND_BATCH_CMD, {
            adminId: adminId,
            cmd: cmd
        }, data => {
            if (complete) complete();
        })
    },
    getLen(paramCode) {
        if (paramCode == 0) {
            return '0007';
        } else if (paramCode == 7) {
            return '000A';
        } else {
            return '0008';
        }
        return '';
    },
    formatValue(paramCode, value, step) {
        if (paramCode == 0) {
            return CommonUtils.str_pad("00", parseInt(value / step).toString(16));
        } else if (paramCode == 7) {
            return CommonUtils.str_pad("00000000", parseInt(value / step).toString(16));
        } else {
            return CommonUtils.str_pad("0000", parseInt(value).toString(16))
        }
        return '';
    },
    formatCmd(deviceId, paramCode, groupCode, key, value, step) {
        var cmd = '5A';
        var verCode = '4D5322'
        verCode += this.getLen(paramCode);
        verCode += CommonUtils.str_pad("00000000", deviceId.toString(16));
        verCode += CommonUtils.str_pad("00", groupCode.toString(16));
        verCode += CommonUtils.str_pad("00", key.toString(16));
        verCode += this.formatValue(paramCode, value, step);
        cmd += verCode;
        cmd += CRC.ToCRC16(CommonUtils.str2Bytes(verCode));
        cmd += '4D';
        return cmd;
    },




}